<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>DB-&gt;open()</title>
    <link rel="stylesheet" href="apiReference.css" type="text/css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
    <link rel="start" href="index.html" title="Berkeley DB C API Reference" />
    <link rel="up" href="db.html" title="Chapter 2.  The DB Handle" />
    <link rel="prev" href="dbkey_range.html" title="DB-&gt;key_range()" />
    <link rel="next" href="dbput.html" title="DB-&gt;put()" />
  </head>
  <body>
    <div xmlns="" class="navheader">
      <div class="libver">
        <p>Library Version 11.2.5.3</p>
      </div>
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">DB-&gt;open()</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="dbkey_range.html">Prev</a> </td>
          <th width="60%" align="center">Chapter 2. 
                The DB Handle
        </th>
          <td width="20%" align="right"> <a accesskey="n" href="dbput.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="sect1" lang="en" xml:lang="en">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title" style="clear: both"><a id="dbopen"></a>DB-&gt;open()</h2>
          </div>
        </div>
      </div>
      <pre class="programlisting">#include &lt;db.h&gt;

int
DB-&gt;open(DB *db, DB_TXN *txnid, const char *file,
    const char *database, DBTYPE type, u_int32_t flags, int mode);  </pre>
      <p>
        The <code class="methodname">DB-&gt;open()</code> method opens the database represented by the 
        <span class="bold"><strong>file</strong></span> and <span class="bold"><strong>database</strong></span>.
    </p>
      <p>
         The currently supported Berkeley DB file formats (or <span class="emphasis"><em>access
         methods</em></span>) are Btree, Hash, Heap, Queue, and Recno.  The Btree
         format is a representation of a sorted, balanced tree structure.  The
         Hash format is an extensible, dynamic hashing scheme.  The Queue
         format supports fast access to fixed-length records accessed
         sequentially or by logical record number.  The Recno format supports
         fixed- or variable-length records, accessed sequentially or by logical
         record number, and optionally backed by a flat text file.
    </p>
      <p>
         Storage and retrieval for the Berkeley DB access methods are based on
         key/data pairs; see <a class="link" href="dbt.html" title="Chapter 4.  The DBT Handle">DBT</a> 
         for more information.
    </p>
      <p>
         Calling <code class="methodname">DB-&gt;open()</code> is a relatively expensive operation, and
         maintaining a set of open databases will normally be preferable to
         repeatedly opening and closing the database for each new query.
    </p>
      <p>
         The <code class="methodname">DB-&gt;open()</code> <span>
            <span>
                  method returns a non-zero error value on failure and 0 on success.
            </span>
            
        </span>
         If <code class="methodname">DB-&gt;open()</code> fails, the 
         <a class="xref" href="dbclose.html" title="DB-&gt;close()">DB-&gt;close()</a>  method must be
         called to discard the <a class="link" href="db.html" title="Chapter 2.  The DB Handle">DB</a> 
         handle.
    </p>
      <div class="sect2" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title"><a id="idp49854872"></a>Parameters</h3>
            </div>
          </div>
        </div>
        <div class="sect3" lang="en" xml:lang="en">
          <div class="titlepage">
            <div>
              <div>
                <h4 class="title"><a id="idp49885248"></a>txnid</h4>
              </div>
            </div>
          </div>
          <p>
                  If the operation is part of an application-specified
                  transaction, the <span class="bold"><strong>txnid</strong></span>
                  parameter is a transaction handle returned from 
                  <a class="xref" href="txnbegin.html" title="DB_ENV-&gt;txn_begin()">DB_ENV-&gt;txn_begin()</a>; if
                  the operation is part of a Berkeley DB Concurrent Data
                  Store group, the
                  <span class="bold"><strong>txnid</strong></span> parameter is a
                  handle returned from 
                  <a class="xref" href="envcdsgroup_begin.html" title="DB_ENV-&gt;cdsgroup_begin()">DB_ENV-&gt;cdsgroup_begin()</a>; 
                  otherwise NULL.  If no transaction handle is specified,
                  but the DB_AUTO_COMMIT flag is specified, the operation
                  will be implicitly transaction protected. Note that
                  transactionally protected operations on a 
                  <a class="link" href="db.html" title="Chapter 2.  The DB Handle">DB</a>  handle requires the
                  <a class="link" href="db.html" title="Chapter 2.  The DB Handle">DB</a>  handle itself be
                  transactionally protected during its open.  Also note
                  that the transaction must be committed before the handle
                  is closed; see 
                  <a href="../../programmer_reference/program_scope.html" class="olink">Berkeley DB handles</a> 
                  for more information.
             </p>
        </div>
        <div class="sect3" lang="en" xml:lang="en">
          <div class="titlepage">
            <div>
              <div>
                <h4 class="title"><a id="idp49895336"></a>file</h4>
              </div>
            </div>
          </div>
          <p>
                  The <span class="bold"><strong>file</strong></span> parameter is used as the
                  name of an underlying file that will be used to back the database; see
                  <a href="../../programmer_reference/env_naming.html" class="olink">File naming</a> for more
                  information.
             </p>
          <p>
                  In-memory databases never intended to be preserved on disk may be
                  created by setting the <span class="bold"><strong>file</strong></span> parameter
                  to NULL. Whether other threads of control can access this database is
                  driven entirely by whether the <span class="bold"><strong>database</strong></span>
                  parameter is set to NULL.
             </p>
          <p>
                     When using a Unicode build on Windows (the default), the <span class="bold"><strong>file</strong></span> argument will be interpreted as a UTF-8 
                     string, which is equivalent to ASCII for Latin characters.
             </p>
        </div>
        <div class="sect3" lang="en" xml:lang="en">
          <div class="titlepage">
            <div>
              <div>
                <h4 class="title"><a id="idp49851072"></a>database</h4>
              </div>
            </div>
          </div>
          <p>
                  The <span class="bold"><strong>database</strong></span> parameter is optional,
                  and allows applications to have multiple databases in a single file. 
                  Although no <span class="bold"><strong>database</strong></span> parameter needs
                  to be specified, it is an error to attempt to open a second database
                  in a <span class="bold"><strong>file</strong></span> that was not initially
                  created using a <span class="bold"><strong>database</strong></span> name. 
                  Further, the <span class="bold"><strong>database</strong></span> parameter is
                  not supported by the Queue format.  Finally, when opening multiple
                  databases in the same physical file, it is important to consider
                  locking and memory cache issues; see 
                  <a href="../../programmer_reference/am_opensub.html" class="olink">Opening multiple databases in a single file</a> 
                  for more information.
             </p>
          <p>
                 If both the <span class="bold"><strong>database</strong></span> and
                 <span class="bold"><strong>file</strong></span> parameters are NULL,
                  the database is strictly temporary and cannot be opened
                  by any other thread of control. Thus the database can only be accessed
                  by sharing the single database handle that created it, in
                  circumstances where doing so is safe.  
              </p>
          <p>
                 If the <span class="bold"><strong>database</strong></span> parameter is not 
                 set to NULL, the database can be opened by other threads of control 
                 and will be replicated to client sites in any replication group, regardless
                 of whether the <span class="bold"><strong>file</strong></span> parameter is set to
                 NULL.
             </p>
        </div>
        <div class="sect3" lang="en" xml:lang="en">
          <div class="titlepage">
            <div>
              <div>
                <h4 class="title"><a id="idp49891112"></a>type</h4>
              </div>
            </div>
          </div>
          <p>
                  The <span class="bold"><strong>type</strong></span> parameter is of
                  type DBTYPE, and must be set to one of
                  <code class="literal">DB_BTREE</code>, <code class="literal">DB_HASH</code>,
                  <code class="literal">DB_HEAP</code>, <code class="literal">DB_QUEUE</code>,
                  <code class="literal">DB_RECNO</code>, or <code class="literal">DB_UNKNOWN</code>.  
                  If <span class="bold"><strong>type</strong></span> is DB_UNKNOWN, the
                  database must already exist and
                  <code class="methodname">DB-&gt;open()</code> will automatically
                  determine its type.  The
                  <a class="xref" href="dbget_type.html" title="DB-&gt;get_type()">DB-&gt;get_type()</a>  method
                  may be used to determine the underlying type of databases opened using
                  DB_UNKNOWN.
             </p>
          <p>
                 It is an error to specify the incorrect <span class="bold"><strong>type</strong></span> 
                 for a database that already exists.
             </p>
        </div>
        <div class="sect3" lang="en" xml:lang="en">
          <div class="titlepage">
            <div>
              <div>
                <h4 class="title"><a id="idp49892184"></a>flags</h4>
              </div>
            </div>
          </div>
          <p>
                The <span class="bold"><strong>flags</strong></span> parameter must be set to zero or
                by bitwise inclusively <span class="bold"><strong>OR</strong></span>'ing together one
                or more of the following values: 
            </p>
          <div class="itemizedlist">
            <ul type="disc">
              <li>
                <p><a id="open_DB_AUTO_COMMIT"></a>
                         <code class="literal">DB_AUTO_COMMIT</code>
                    </p>
                <p>
                        Enclose the <code class="methodname">DB-&gt;open()</code>
                        call within a transaction. If the call succeeds,
                        the open operation will be recoverable and all
                        subsequent database modification operations based
                        on this handle will be transactionally protected.
                        If the call fails, no database will have been
                        created.
                    </p>
              </li>
              <li>
                <p><a id="open_DB_CREATE"></a>
                         <code class="literal">DB_CREATE</code>
                    </p>
                <p>
                        Create the database. If the database does not
                        already exist and the <code class="literal">DB_CREATE</code>
                        flag is not specified, the
                        <code class="methodname">DB-&gt;open()</code> will fail.
                    </p>
              </li>
              <li>
                <p><a id="open_DB_EXCL"></a>
                         <code class="literal">DB_EXCL</code>
                    </p>
                <p>
                        Return an error if the database already exists. The
                        <code class="literal">DB_EXCL</code> flag is only meaningful 
                        when specified with the <code class="literal">DB_CREATE</code>.
                        flag.
                    </p>
              </li>
              <li>
                <p><a id="dbopen_DB_MULTIVERSION"></a>
                         <code class="literal">DB_MULTIVERSION</code>
                    </p>
                <p>
                        Open the database with support for 
                        <a href="../../programmer_reference/transapp_read.html" class="olink">
                            multiversion concurrency control</a>. 
                        This will cause updates to the database to follow a
                        copy-on-write protocol, which is required to
                        support snapshot isolation. The
                        <code class="literal">DB_MULTIVERSION</code> flag requires
                        that the database be transactionally protected
                        during its open and is not supported by the queue
                        format.
                    </p>
              </li>
              <li>
                <p><a id="open_DB_NOMMAP"></a>
                             <code class="literal">DB_NOMMAP</code>
                        </p>
                <p>
                            Do not map this database into process memory (see the
                            <a class="xref" href="envset_mp_mmapsize.html" title="DB_ENV-&gt;set_mp_mmapsize()">DB_ENV-&gt;set_mp_mmapsize()</a>  
                            method for further information).
                        </p>
              </li>
              <li>
                <p><a id="dbopen_DB_RDONLY"></a>
                            <code class="literal">DB_RDONLY</code>
                        </p>
                <p>
                            Open the database for reading only. Any attempt to modify
                            items in the database will fail, regardless of the actual
                            permissions of any underlying files.
                        </p>
              </li>
              <li>
                <p><a id="dbopen_DB_READ_UNCOMMITTED"></a>
                            <code class="literal">DB_READ_UNCOMMITTED</code>
                        </p>
                <p>
                            Support transactional read operations with degree 1
                            isolation. Read operations on the database may request the
                            return of modified but not yet committed data. This flag
                            must be specified on all 
                            <a class="link" href="db.html" title="Chapter 2.  The DB Handle">DB</a>  
                            handles used to perform dirty
                            reads or database updates, otherwise requests for dirty
                            reads may not be honored and the read may block.
                        </p>
              </li>
              <li>
                <p><a id="open_DB_THREAD"></a>
                            <code class="literal">DB_THREAD</code>
                        </p>
                <p>
                            Cause the <a class="link" href="db.html" title="Chapter 2.  The DB Handle">DB</a>
                            handle returned by
                            <code class="methodname">DB-&gt;open()</code> to be
                            <span class="emphasis"><em>free-threaded</em></span>; that is,
                            concurrently usable by multiple threads in the
                            address space.
                        </p>
                <p>
                            Note that this flag is incompatible with the 
                            <a class="link" href="dbset_lk_exclusive.html" title="DB-&gt;set_lk_exclusive()">DB-&gt;set_lk_exclusive()</a>
                            method.
                        </p>
              </li>
              <li>
                <p><a id="open_DB_TRUNCATE"></a>
                            <code class="literal">DB_TRUNCATE</code>
                        </p>
                <p>
                            Physically truncate the underlying file, discarding all
                            previous databases it might have held. Underlying filesystem
                            primitives are used to implement this flag. For this reason,
                            it is applicable only to the file and cannot be used to
                            discard databases within a file.
                        </p>
                <p>
                            The <code class="literal">DB_TRUNCATE</code> flag cannot be lock or
                            transaction-protected, and it is an error to specify it in a
                            locking or transaction-protected environment.
                        </p>
              </li>
            </ul>
          </div>
        </div>
        <div class="sect3" lang="en" xml:lang="en">
          <div class="titlepage">
            <div>
              <div>
                <h4 class="title"><a id="idp49923776"></a>mode</h4>
              </div>
            </div>
          </div>
          <p>
                  On Windows systems, the mode parameter is ignored.                    
             </p>
          <p>
                  On UNIX systems or in IEEE/ANSI Std 1003.1 (POSIX) environments, files
                  created by the database open are created with mode <span class="bold"><strong>mode</strong></span> (as described in <span class="bold"><strong>chmod</strong></span>(2)) and modified by the process' umask
                  value at the time of creation (see <span class="bold"><strong>umask</strong></span>(2)).  Created files are owned by the
                  process owner; the group ownership of created files is based on the
                  system and directory defaults, and is not further specified by
                  Berkeley DB.  System shared memory segments created by the database
                  open are created with mode <span class="bold"><strong>mode</strong></span>,
                  unmodified by the process' umask value.  If <span class="bold"><strong>mode</strong></span> is 0, the database open will use a default
                  mode of readable and writable by both owner and group.
             </p>
        </div>
      </div>
      <div class="sect2" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title"><a id="idp49898440"></a>Environment Variables</h3>
            </div>
          </div>
        </div>
        <p>
             If the database was opened within a database environment, the
             environment variable <span class="bold"><strong>DB_HOME</strong></span>
             may be used as the path of the database environment home.
        </p>
        <p>
            <code class="methodname">DB-&gt;open()</code> is affected by any
            database directory specified using the 
            <a class="xref" href="envset_data_dir.html" title="DB_ENV-&gt;set_data_dir()">DB_ENV-&gt;set_data_dir()</a> 
            method, or by setting the "set_data_dir" string in the
            environment's <a href="../../programmer_reference/env_db_config.html#env_db_config.DB_CONFIG" class="olink">DB_CONFIG</a> file.
        </p>
        <div class="itemizedlist">
          <ul type="disc">
            <li>
              <p>
                    <span class="bold"><strong>TMPDIR</strong></span>
                </p>
              <p>
                    If the <span class="bold"><strong>file</strong></span> and
                    <span class="bold"><strong>dbenv</strong></span> parameters to
                    <code class="methodname">DB-&gt;open()</code> are
                    <code class="literal">NULL</code>, the environment variable
                    <span class="bold"><strong>TMPDIR</strong></span> may be used as
                    a directory in which to create temporary backing files
                </p>
            </li>
          </ul>
        </div>
      </div>
      <div class="sect2" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title"><a id="idp49943400"></a>Errors</h3>
            </div>
          </div>
        </div>
        <p>
             The <code class="methodname">DB-&gt;open()</code> <span>
            <span>
                 method may fail and return one of the following non-zero errors:
            </span>
            
        </span>
        </p>
        <div class="sect3" lang="en" xml:lang="en">
          <div class="titlepage">
            <div>
              <div>
                <h4 class="title"><a id="idp49888936"></a>DB_LOCK_DEADLOCK</h4>
              </div>
            </div>
          </div>
          <p>
                A transactional database environment operation was selected to resolve
                a deadlock.
            </p>
        </div>
        <div class="sect3" lang="en" xml:lang="en">
          <div class="titlepage">
            <div>
              <div>
                <h4 class="title"><a id="idp49925152"></a>DB_LOCK_NOTGRANTED</h4>
              </div>
            </div>
          </div>
          <p>
                A Berkeley DB Concurrent Data Store database environment configured
                for lock timeouts was unable to grant a lock in the allowed time.
            </p>
          <p>
                You attempted to open a database handle that is configured
                for no waiting exclusive locking, but the exclusive lock could not be
                immediately obtained. See
                <a class="xref" href="dbset_lk_exclusive.html" title="DB-&gt;set_lk_exclusive()">DB-&gt;set_lk_exclusive()</a>
                for more information.
            </p>
        </div>
        <div class="sect3" lang="en" xml:lang="en">
          <div class="titlepage">
            <div>
              <div>
                <h4 class="title"><a id="idp49896304"></a>ENOENT</h4>
              </div>
            </div>
          </div>
          <p>
                The file or directory does not exist.
            </p>
        </div>
        <div class="sect3" lang="en" xml:lang="en">
          <div class="titlepage">
            <div>
              <div>
                <h4 class="title"><a id="idp49946368"></a>ENOENT</h4>
              </div>
            </div>
          </div>
          <p>
                A nonexistent <span class="bold"><strong>re_source</strong></span> file was specified.
            </p>
        </div>
        <div class="sect3" lang="en" xml:lang="en">
          <div class="titlepage">
            <div>
              <div>
                <h4 class="title"><a id="idp49940616"></a>DB_OLD_VERSION</h4>
              </div>
            </div>
          </div>
          <p>
                The database cannot be opened without being first upgraded.
            </p>
        </div>
        <div class="sect3" lang="en" xml:lang="en">
          <div class="titlepage">
            <div>
              <div>
                <h4 class="title"><a id="idp49947592"></a>EEXIST</h4>
              </div>
            </div>
          </div>
          <p>
                <code class="literal">DB_CREATE</code> and <code class="literal">DB_EXCL</code>
                were specified and the database exists.
            </p>
        </div>
        <div class="sect3" lang="en" xml:lang="en">
          <div class="titlepage">
            <div>
              <div>
                <h4 class="title"><a id="idp49941952"></a>EINVAL</h4>
              </div>
            </div>
          </div>
          <p>
               If an unknown database type, page size, hash function, pad
               byte, byte order, or a flag value or parameter that is
               incompatible with the specified database was specified; the
               <a class="link" href="envopen.html#envopen_DB_THREAD">DB_THREAD</a> 
               flag was specified and fast mutexes are not available for
               this architecture; the 
               <a class="link" href="envopen.html#envopen_DB_THREAD">DB_THREAD</a> 
               flag was specified to <code class="methodname">DB-&gt;open()</code>,
               but was not specified to the
               <code class="methodname">DB_ENV-&gt;open()</code> call for the
               environment in which the 
               <a class="link" href="db.html" title="Chapter 2.  The DB Handle">DB</a> handle was created; a
               backing flat text file was specified with either the 
               <a class="link" href="envopen.html#envopen_DB_THREAD">DB_THREAD</a> 
               flag or the provided database environment supports
               transaction processing; a Heap database is in use and 
               <a class="xref" href="dbset_heapsize.html" title="DB-&gt;set_heapsize()">DB-&gt;set_heapsize()</a>
               was used to set a heap size that is different from the value
               used to create the database or an invalid heap region size was
	       set using <a class="xref" href="dbset_heap_regionsize.html" title="DB-&gt;set_heap_regionsize()">DB-&gt;set_heap_regionsize()</a>; 
	       or if an invalid flag value or parameter was specified.
         </p>
        </div>
        <div class="sect3" lang="en" xml:lang="en">
          <div class="titlepage">
            <div>
              <div>
                <h4 class="title"><a id="idp49940832"></a>  DB_REP_HANDLE_DEAD</h4>
              </div>
            </div>
          </div>
          <p>
                When a client synchronizes with the master, it is possible for committed
                transactions to be rolled back. This invalidates all  the database and cursor
                handles opened in the replication environment. Once this occurs, an attempt to use
                such a handle will 
                
                return <code class="literal">DB_REP_HANDLE_DEAD</code>.
                The application will need to discard the handle and open a new one in order to
                continue processing.
            </p>
        </div>
        <div class="sect3" lang="en" xml:lang="en">
          <div class="titlepage">
            <div>
              <div>
                <h4 class="title"><a id="idp49921784"></a>DB_REP_LOCKOUT</h4>
              </div>
            </div>
          </div>
          <p>
                The operation was blocked by client/master synchronization.
            </p>
        </div>
      </div>
      <div class="sect2" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title"><a id="idp49920752"></a>Class</h3>
            </div>
          </div>
        </div>
        <p>
            <a class="link" href="db.html" title="Chapter 2.  The DB Handle">DB</a>  
        </p>
      </div>
      <div class="sect2" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title"><a id="idp49935408"></a>See Also</h3>
            </div>
          </div>
        </div>
        <p>
             <a class="xref" href="db.html#dblist" title="Database and Related Methods">Database and Related Methods</a> 
        </p>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="dbkey_range.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="db.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="dbput.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">DB-&gt;key_range() </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> DB-&gt;put()</td>
        </tr>
      </table>
    </div>
  </body>
</html>
